package com.zt.douyamusic.activity;

import android.app.Dialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.kongzue.dialog.listener.InputDialogOkButtonClickListener;
import com.kongzue.dialog.v2.InputDialog;
import com.kongzue.dialog.v2.MessageDialog;
import com.zt.douyamusic.R;
import com.zt.douyamusic.bmob.Message;

import java.util.List;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Author: ZT on 2019/2/18
 */

public class InfoActivity extends InfoBaseActivity implements View.OnClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);
        findViewById(R.id.user_info_ll).setOnClickListener(this);
        findViewById(R.id.about_our_ll).setOnClickListener(this);
        findViewById(R.id.yjfk_ll).setOnClickListener(this);
        findViewById(R.id.clear_cache_ll).setOnClickListener(this);
        findViewById(R.id.login_close_tv).setOnClickListener(this);
        findViewById(R.id.login_out_tv).setOnClickListener(this);
    }
    /**
     * 禁用侧滑
     */
    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.translate_none,R.anim.translate_between_to_down);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.user_info_ll:
                MessageDialog.show(this, "提示", "作者QQ:1356798719", "复制", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ClipboardManager copy = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
                        copy.setText("1356798719");
                        Toast.makeText(InfoActivity.this, "复制成功，登陆QQ搜索即可", Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case R.id.about_our_ll:
                openQQ();
                break;
            case R.id.yjfk_ll:
                fk();
                break;
            case R.id.clear_cache_ll:
                Toast.makeText(this, "清除成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.login_close_tv:
                finish();
                break;
            case R.id.login_out_tv:
                BmobUser.logOut();
                finish();
                break;
        }
    }
    private void goActivity(Class activityClass,String url){
        Intent intent = new Intent(this,activityClass);
        intent.putExtra("url", url);
        startActivity(intent);
    }
    private void fk(){
        InputDialog.show(this, "反馈", "意见反馈", "提交", new InputDialogOkButtonClickListener() {
            @Override
            public void onClick(final Dialog dialog, String inputText) {
                Message message = new Message();
                message.msg = inputText;
                message.save(new SaveListener<String>() {
                    @Override
                    public void done(String s, BmobException e) {
                        dialog.dismiss();
                        if(e==null){
                            Toast.makeText(InfoActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }, "取消", null);
    }
    public void openQQ(){
        // 跳转之前，可以先判断手机是否安装QQ
        if (isQQClientAvailable(this)) {
            // 跳转到客服的QQ
            String url = "mqqwpa://im/chat?chat_type=wpa&uin=1356798719";
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            // 跳转前先判断Uri是否存在，如果打开一个不存在的Uri，App可能会崩溃
            if (isValidIntent(this,intent)) {
                startActivity(intent);
            }
        }else {
            Toast.makeText(this,"你手机还没有安装QQ，请安装后在试",Toast.LENGTH_SHORT).show();
        }
    }
    /**
     * 判断 用户是否安装QQ客户端
     */
    public static boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equalsIgnoreCase("com.tencent.qqlite") || pn.equalsIgnoreCase("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }
    /**
     * 判断 Uri是否有效
     */
    public static boolean isValidIntent(Context context, Intent intent) {
        PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
        return !activities.isEmpty();
    }
}
